package com.zhangyu.shadow.ec.main.sort.list;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.tepth.shadow.ui.recycler.ItemType;
import com.tepth.shadow.ui.recycler.MultipleField;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;
import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.main.sort.SortDelegate;
import com.zhangyu.shadow.ec.main.sort.content.ContentDelegate;

import java.util.List;

import me.yokeyword.fragmentation.SupportHelper;

/**
 * @author zhangyu
 * @date 2017/11/7
 */

public class VerticalListAdapter extends MultipleRecyclerAdapter {
    private final SortDelegate mSortDelegate;
    private int mPrePosition = 0;

    protected VerticalListAdapter(List<MultipleItemEntity> data, SortDelegate sortDelegate) {
        super(data);
        mSortDelegate = sortDelegate;
        //添加垂直布局菜单
        addItemType(ItemType.VERTICAL_MENU_LIST, R.layout.item_vertical_menu_list);
    }

    @Override
    protected void convert(final MultipleViewHolder helper, final MultipleItemEntity item) {
        super.convert(helper, item);
        switch (helper.getItemViewType()) {
            case ItemType.VERTICAL_MENU_LIST:
                final String name = item.getField(MultipleField.NAME);
                final boolean isChecked = item.getField(MultipleField.TAG);
                helper.setVisible(R.id.view_line, isChecked);
                helper.setText(R.id.tv_vertical_item_name, name);
                final View itemView = helper.itemView;
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int currentPosition = helper.getAdapterPosition();
                        if (mPrePosition != currentPosition) {
                            getData().get(mPrePosition).setField(MultipleField.TAG, false);
                            notifyItemChanged(mPrePosition);

                            item.setField(MultipleField.TAG, true);
                            notifyItemChanged(currentPosition);

                            mPrePosition = currentPosition;
                            final int contentId = getData().get(currentPosition)
                                    .getField(MultipleField.ID);
                            showContent(contentId);
                        }
                    }
                });
                if (!isChecked) {
                    helper.setTextColor(R.id.tv_vertical_item_name,
                            ContextCompat.getColor(mContext, R.color.we_chat_black));
                    itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.item_background));
                } else {
                    helper.setTextColor(R.id.tv_vertical_item_name,
                            ContextCompat.getColor(mContext, R.color.app_main));
                    itemView.setBackgroundColor(Color.WHITE);
                }
                break;
            default:

                break;
        }
    }

    private void showContent(int contentId) {
        final ContentDelegate delegate = ContentDelegate.create(contentId);
        switchContent(delegate);
    }

    private void switchContent(ContentDelegate delegate) {
        //  final ShadowDelegate contentDelegate = mSortDelegate.findChildFragment(ContentDelegate.class);
        final ShadowDelegate contentDelegate = SupportHelper.findFragment(mSortDelegate.getChildFragmentManager(),
                ContentDelegate.class);
        if (contentDelegate != null) {
            contentDelegate.replaceFragment(delegate, false);
        }
    }
}
